package com.huanling.main.utils;

import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.util.Pair;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件选择器工具
 *
 * @author guibo.sun
 * @since 23 七月 2021
 */
public class FileChooserUtil {

    private FileChooserUtil() {
    }

    public static File openFileChooser(Stage primaryStage, List<Pair<String, String>> extensions) {
        FileChooser fileChooser = new FileChooser();
        for (Pair<String, String> extension : extensions) {
            FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter(extension.getKey(), extension.getValue());
            fileChooser.getExtensionFilters().add(filter);
        }
        return fileChooser.showOpenDialog(primaryStage);
    }

    public static List<Pair<String, String>> buildExcelFileExtensionPairs() {
        List<Pair<String, String>> fileExtensions = new ArrayList<>();
        fileExtensions.add(new Pair<>("XLSX (*.xlsx)", "*.xlsx"));
        fileExtensions.add(new Pair<>("XLS (*.xls)", "*.xls"));
        return fileExtensions;
    }
}
